<?php 


class Blog extends ActiveRecord {
	
	
	private $code;
    // Inherits $tablename from ActiveRecord
    // Inherits $dbobj from ActiveRecord
	
	
	public function __construct($id=false) {
		
		$this->tablename = 'links';
		
		if ($id) {
			$this->dbobj->id = $id;
			
			// It's not necessary to do a query every new blog, we already have the data
			//$this->get();
		}
		
	}
	
	
	public function getBlogHtml($url) {
	
	  $ch = curl_init();
	  curl_setopt($ch, CURLOPT_URL, $url);
	  curl_setopt($ch, CURLOPT_HEADER, false);
	  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,15);
	  curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	  curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com/');
	
	  $code = curl_exec($ch);
	  $error = curl_errno($ch);
	  curl_close($ch);
	  
	  if (!empty($error)) {
	      return false;
	  }
	
	  $this->code = $code;
	  
	  return true;
	}
	
	
	public function getEntries($lastlang='', $langs=array(), $head) {
	
	    $entries = $this->explodeEntries();
	    $lang = $this->getLang($entries[0], $lastlang);
	
	    $system = new System();
	    $langid = $system->getLangId($lang);
	
	    foreach ($entries as $entrycode) {
	    	
	    	$entry = new Entry($entrycode);
	        
	        $entrytitle = $entry->getTitle();
	        if ($entrytitle) {
	        	
	            $entry->dbobj->url = iconv("UTF-8", "ISO-8859-1", $entrytitle['url']);
	            $entry->dbobj->title = iconv("UTF-8", "ISO-8859-1", $entrytitle['title']);
                $entry->dbobj->content = iconv("UTF-8", "ISO-8859-1", $entry->getContent());
                $entry->dbobj->contenttoshow = iconv("UTF-8", "ISO-8859-1", $entry->getCleanContent($content));
	            $entry->dbobj->date = $entry->getDate($entrytitle['url']);
	            $entry->dbobj->timeadded = time();
	            
	            $entryid = $entry->insert();
	        }
	
	        // If has been inserted into DB
	        if ($entryid) {
	        	
	        	// Extract media and save
	        	$entrymedias = new EntryMedias($entryid);
	        	
                $entrymedias->getMedias();
	            $entrymedias->saveMedias();
	            
	            // Extract keywords and save
	            $entrykeywords = new EntryKeyWords();
	            
                $labels = $entry->getLabels();
	            $entrykeywords->getKeyWords($content, $labels, $entrytitle, $head, $langid);
	            $entrykeywords->saveKeyWords($entryid, $keywords, $langid);
	            
	        }
	    }
	
	    return $lang;
	}
	
	
	
	public function getLang($html, $lastlang='') {
	
	    global $CONFIG;
	
	    include($CONFIG->datadir.'/lib/langs.php');
	
	    if(!empty($lastlang) && $lastlang != ''){
	      foreach($langs[$lastlang]["months"] as $lastlangmonth){
	        if(strstr(strtolower($html), strtolower($lastlangmonth).',') != false OR strstr(strtolower($html), strtolower($lastlangmonth).' ') != false){
	            return $lastlang;
	        }
	      }
	      foreach($langs[$lastlang]["days"] as $lastlangday){
	        if(strstr(strtolower($html), strtolower($lastlangday).',') != false OR strstr(strtolower($html), strtolower($lastlangday).' ') != false){
	            return $lastlang;
	        }
	      }
	    }
	
	    unset($langs[$lastlang]);
	    foreach($langs as $key=>$lang){
	        foreach($lang['months'] as $month){
	            if(strstr(strtolower($html), strtolower($month).',') != false OR strstr(strtolower($html), strtolower($month).' ') != false){
	                return $key;
	            }
	        }
	        foreach($lang['days'] as $day){
	            if(strstr(strtolower($html), strtolower($day).',') != false OR strstr(strtolower($html), strtolower($day).' ') != false){
	                return $key;
	            }
	        }
	    }
	
	    return $lastlang;
	}
	
	
	
	public function saveLinks($linkname) {
	
	    global $DB;
	
	    $now = time();
	    if(empty($pagesstring)){
	    	$pagesstring = '';
	    }
	
	    preg_match_all("#<a href=(.*?)>#", $this->code, $alllinks);
	    if(!empty($alllinks[1])){
	        foreach($alllinks[1] as $link){
	            if(strstr($link, $linkname) == false && strstr($link, 'blogspot.com') != false){
	                $cleanlink = str_replace("'", "", str_replace('"', '', $link));
	                $spacepos = strpos($cleanlink, ' ', 7);
	                $quotepos = strpos($cleanlink, '/', 7);
	                if($spacepos != false){
	                    $cleanlink = substr($cleanlink, 0, $spacepos);
	                }
	                if($quotepos != false){
	                    $cleanlink = substr($cleanlink, 0, $quotepos);
	                }
	                $pagename = str_replace('http://', '', str_replace('.blogspot.com', '', $cleanlink));
	                $sql = "SELECT id FROM links WHERE name LIKE ? OR url LIKE ?";
	                
	                if (!$DB->selectResponse($sql, array($pagename, $cleanlink))) {
	                	
	                	$newlink = new Blog();
	                	$newlink->dbobj->url = $cleanlink;
	                	$newlink->dbobj->name = $pagename;
	                	$newlink->insert();
	                }
	            }
	        }
	    }
	}


	public function domClean() {
	
	  $this->code = preg_replace("/<link(.*?)>/", "", $this->code);
	  $this->code = preg_replace("/\n/", " ", $this->code);
	  $this->code = preg_replace("/<style(.*?)style>/", "", $this->code);
	  $this->code = preg_replace("/style=\'(.*?)\'/", "", $this->code);
	  $this->code = preg_replace("/style=\"(.*?)\"/", "", $this->code);
	  $this->code = preg_replace("/<!--(.*?)-->/", "", $this->code);
	  $this->code = preg_replace("/<script(.*?)script>/", "", $this->code);
	  //$this->code = preg_replace("/<head(.*?)head>/", "", $this->code);
	
	  /*$dom = new DomDocument;
	  //$dom->strictErrorChecking = false;
	  $dom->LoadHTML($this->code);
	  $navbar = $dom->getElementById('navbar');
	  $navbarchild = $dom->getElementById('Navbar1');
	  if($navbar){
	    $navbar->removeChild($navbarchild);
	  }
	  $parent = $dom->getElementById('wrap2');
	  $skiplinkschild = $dom->getElementById('skiplinks');
	  $headerchild = $dom->getElementById('header-wrapper');
	  $footerchild = $dom->getElementById('footer-wrapper');
	  if($parent){
	    $parent->removeChild($headerchild);
	    $parent->removeChild($footerchild);
	    $parent->removeChild($skiplinkschild);
	  }
	  $contentparent = $dom->getElementById('content-wrapper');
	  $sidebarchild = $dom->getElementById('sidebar-wrapper');
	  $crosscolchild = $dom->getElementById('crosscol-wrapper');
	  if($contentparent){
	    $contentparent->removeChild($sidebarchild);
	    $contentparent->removeChild($crosscolchild);
	  }
	  $blog1 = $dom->getElementById('Blog1');
	  $blogpaperchild = $dom->getElementById('blog-pager');
	  if($blog1){
	    $blog1->removeChild($blogpaperchild);
	  }
	  $this->code = $dom->saveHTML();*/
	
	}


	private function explodeEntries() {
	
	    $entries = explode('<h3', $this->code);
	    unset($entries[0]);
	
	    foreach($entries as $key=>$entry){
	        $entries[$key] = '<h3'.$entries[$key];
	    }
	
	    $string = implode('|||', $entries);
	    $entries = explode('|||', $string);
	
	    return $entries;
	}

	
	public function Visited() {
		
		$this->dbobj->timevisited = time();
		$this->update();
	}
}

?>